package com.zja.servlet;


import com.zja.model.JsonResponse;
import com.zja.service.ArticleViewHistoryService;
import com.zja.service.Impl.ArticleViewHistoryServiceImpl;
import com.zja.util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author JA
 * @date: 2025/5/7
 * description:删除所有浏览记录
 */
@WebServlet("/viewHistory/deleteAll")
public class DeleteAllViewHistoryServlet extends HttpServlet {
    private final ArticleViewHistoryService viewHistoryService = new ArticleViewHistoryServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession(false);
        Long authorId = (session != null) ? (Long) session.getAttribute("userId") : null;

        if (authorId == null) {
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("未登录，无法删除记录"));
            return;
        }

        int deletedCount = viewHistoryService.deleteAllViewRecordsOfMyArticles(authorId);

        if (deletedCount > 0) {
            ServletUtil.writeJsonResponse(resp, JsonResponse.success("成功删除 " + deletedCount + " 条浏览记录"));
        } else {
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("没有记录被删除或删除失败"));
        }
    }
}
